<template>
  <div class="dialog_content">
    <el-dialog class="m_dialog" :title="dialogTitles[titleIndex] || titleIndex" :visible.sync="dialogVisible" width="800" append-to-body :before-close="cancel" :close-on-click-modal="false">
      <div class="content">
        <custom-form ref="form" formRef="form" :formModel="formModel" :formItemConfig="formItemConfig" :formRules="formRules" label-width="80px" label-position="left" />
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel" class="btn_cancel">取 消</el-button>
        <el-button type="primary" @click="submitFrom()" class="btn_primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addProjectPointType, updateProjectPointType } from '@/api/projectPoints/projectType';
import { mapState } from 'vuex';
export default {
  name: 'ProjectPointTypeForm',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    titleIndex: {
      type: Number | String,
      default: 0,
    },
    formData: {
      type: Object,
      default: () => {},
    },
    dictMap: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      // 提交表单数据
      formModel: {
        projectPointTypeId: '',
        projectPointTypeName: '',
        createTime: '',
        remark: '',
      },
      // 表单配置项
      formItemConfig: [
        // {
        //   label: '项目点类型编号',
        //   prop: 'projectPointTypeId',
        //   span: 12,
        //   clearable: true,
        // },
        {
          label: '项目点类型名称',
          prop: 'projectPointTypeName',
          span: 24,
          clearable: true,
          labelWidth: '150px'
        },
        // {
        //   label: '创建时间',
        //   prop: 'createTime',
        //   span: 24,
        //   clearable: true,
        //   disabled: true,
        //   labelWidth: '150px'
        // },
        {
          label: '备注',
          prop: 'remark',
          span: 24,
          clearable: true,
          labelWidth: '150px'
        },
      ],
      // 表单验证
      formRules: {
        projectPointTypeName: [{ required: true, message: '项目点类型名称不能为空', trigger: 'blur' }]
      },
    };
  },
  computed: {
    ...mapState('dialog', ['dialogTitles']),
  },
  watch: {
    formData: {
      deep: true,
      immediate: true,
      handler() {
        this.formModel = JSON.parse(JSON.stringify(this.formData));
      },
    },
  },
  created() {},
  mounted() {},
  methods: {
    getList() {
      this.$emit('getList');
    },
    cancel() {
      this.$emit('cancel');
      this.$refs["form"].toResetFields();
    },
    // 提交
    submitFrom() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.$utils.isNotEmpty(this.formModel.projectPointTypeId)) {
            updateProjectPointType(this.formModel).then(res => {
              if (200 === res.code) {
                this.$message({
                  message: '修改成功',
                  type: 'success',
                });
                this.cancel();
                this.getList();
              }
            });
          } else {
            addProjectPointType(this.formModel).then(res => {
              if (200 === res.code) {
                this.$message({
                  message: '添加成功',
                  type: 'success',
                });
                this.cancel();
                this.getList();
              }
            });
          }
        } else {
        }
      });
    },
  },
  components: {},
};
</script>

<style lang="less" scoped></style>
